package com.ff.device.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class DeviceUtil {

    private static final Logger logger = LoggerFactory
            .getLogger(DeviceUtil.class);

    public static boolean connectedToDevice(String deviceIp){
        String command="adb connect "+deviceIp;
        return deviceStatusCheck(command,new String[]{"connected","already"});
    }

    public static boolean disConnectedToDevice(String deviceIp){
        String command="adb disconnect "+deviceIp;
        return deviceStatusCheck(command,new String[]{"disconnected","error"});
    }

    /**
     * 设备状态检测
     * @param ip
     * @param reg
     * @return
     */
    public static boolean deviceStatusCheck(String ip,String... reg){
        String line = null;
        BufferedReader bufferedReader=CmdUtils.exeCmd(ip);
        try {
            while ((line = bufferedReader.readLine()) != null) {
                logger.info(line);
                for (int i=0;i<reg.length;i++){
                    if (line.contains(reg[i])) {
                        return true;
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (bufferedReader!=null){
                    bufferedReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }


    /**
     * 获取已连接设备列表
     * @return
     */
    public static List<String> getDeviceList(){
        String command="adb devices";
        String line = null;
        List<String> deviceList=new ArrayList<>();
        String reg="device";
        BufferedReader bufferedReader=null;
        try {
            bufferedReader=CmdUtils.exeCmd(command);
            while ((line = bufferedReader.readLine()) != null) {
                if (line.contains(reg)) {
                    String[] devices=line.split("\t");
                    if (devices.length>=2){
                        deviceList.add(devices[0]);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (bufferedReader!=null){
                    bufferedReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return deviceList;
    }

    public static String getDevid(String ip){
        String command="adb -s "+ip +" shell cat /data/devinfo.txt ";
        try {
            BufferedReader bufferedReader= CmdUtils.exeCmd(command);
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                if (line.contains("devid")) {
                    String[] devices=line.split("=");
                    return devices[1];
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
